package com.link.base.base.performance.service;

import com.link.base.base.performance.dao.mybatis.mapper.ProjectPartBonusMapper;
import com.link.base.base.performance.model.ProjectPartBonus;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * 项目零件奖金
 *
 * @author 王昊
 * @date 2021-04-08 13:20:51
 */
@Service
public class ProjectPartBonusServiceImpl extends BasicServiceImpl<ProjectPartBonus> implements ProjectPartBonusService {

    public static BigDecimal zero = new BigDecimal(0);
    @Resource
    private ProjectPartBonusMapper    projectPartBonusMapper;

    @Override
    public BasicMapper<ProjectPartBonus> getBasicMapper() {
        return projectPartBonusMapper;
    }

    /**
     * 项目零件奖金的所有计算都写在这里
     * @param entity
     * @throws Exception
     */
    @Override
    public void beforUpsert(ProjectPartBonus entity) throws Exception {
        //先判断各项因子是否为空
        //模具设变金额
        BigDecimal modChangeMoney = changeNullToZero(entity.getModChangeMoney());
        //模具设变成本
        BigDecimal modChangeCost = changeNullToZero(entity.getModChangeCost());
        //模具设变奖励系数
        BigDecimal modChangeCoeff = changeNullToZero(entity.getModChangeCoeff());
        //零件设变金额
        BigDecimal partChangeMoney = changeNullToZero(entity.getPartChangeMoney());
        //零件设变成本
        BigDecimal partChangeCost = changeNullToZero(entity.getPartChangeCost());
        //零件设变奖励系数
        BigDecimal partChangeCoeff = changeNullToZero(entity.getPartChangeCoeff());


        //模具设变差额：模具设变金额-模具设变成本。
        entity.setModChangeDiff(modChangeMoney.subtract(modChangeCost));
        //模具设变奖励金额：模具设变差额*模具设变奖励系数。
        entity.setModChangeBonus(entity.getModChangeDiff().multiply(modChangeCoeff));
        //零件设变差额：零件设变金额-零件设变成本。
        entity.setPartChangeDiff(partChangeMoney.subtract(partChangeCost));
        //零件设变奖励金额：零件设变差额*零件设变奖励系数。
        entity.setPartChangeBonus(entity.getPartChangeDiff().multiply(partChangeCoeff));
        projectPartBonusMapper.update(entity);
    }

    /**
     * 判断因子是否为空，空值赋值0
     */
    public BigDecimal changeNullToZero(BigDecimal number) throws Exception {
        if (number == null) {
            return zero;
        }
        return number;
    }
}